//
//  ViewController.m
//  导航栏 渐变
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UIImageView *picImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, 64)];
    _picImageView.image = [UIImage imageNamed:@"tts-navbar"];
    [self.navigationController.navigationBar addSubview:self.picImageView];
    self.navigationItem.title = @"标题";
    
    [self createTableHeadView];
    [self createTableView];

}
- (void)createTableHeadView {
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.backImageView = [[UIImageView alloc] initWithFrame:self.headView.bounds];
    self.backImageView.image = [UIImage imageNamed:@"1(1).jpg"];
    [self.headView addSubview:self.backImageView];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT + 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.tableHeaderView = self.headView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 77;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    CGFloat alp = (y + 64) / 200.0f;
    self.picImageView.alpha = alp;
    
    if (y < -64) {
        CGFloat height = - (y + 64);
        CGFloat scale = (200 + height) / 200.0f;
        self.backImageView.frame = CGRectMake(-(WIDTH * scale - WIDTH) / 2.f, y, WIDTH * scale, 200 - y);
    } else {
//    self.backImageView.frame = CGRectMake(0, 0, WIDTH, 200 );
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];

    cell.textLabel.text = @"哈哈哈";
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
